<?php

namespace backend\controllers;

use common\models\About;
use Yii;

class AboutController extends DashboardController
{
    # 列表页
    public function actionIndex()
    {
        $data = About::find()->asArray()->one();
        return $this->render('index',compact('data'));
    }

    # 保存方法
    public function actionSave()
    {
        $post = Yii::$app->request->post();
        $item = !empty($post['id']) ? About::find()->where(['id' => $post['id']])->one() : new About();

        $saveData = [
            'title' => !empty($post['title']) ? $post['title'] : '',
            'desc' => !empty($post['desc']) ? $post['desc'] : '',
        ];
        $item->attributes = $saveData;
        if ($item->validate()){
            $msg = $item->save() ? ["state" =>"success","message" =>"恭喜小主，操作成功！"] : ["state" => "error","message" => "小主大人，操作失败啦！"];
            $this->ajaxReturn($msg);
        }else{
            $this->ajaxReturn(["state" => 'error',"message" => '验证失败']);
        }
    }
}
